package org.dandsoft.blackbox.labs.snn;

import java.io.Writer;
import java.io.IOException;

/**
 * User: dvilyuzhanin
 * Date: 23.01.2009 13:59:24
 */
@Deprecated
public class NeuronStructureWriter {

    public final static char END_TAG = '\n';
    public final static char SEPARATOR = ';';
    public final static char VALUE_SEPARATOR = ',';
    public final static char SIZE_SEPARATOR = ':';
    private Writer out;
    private int layersCount = 0;

    public NeuronStructureWriter(Writer out) {
        this.out = out;
    }



    public synchronized void write(NeuronLayerGenerator layer) throws IOException {
        int[][] synapses = layer.getSynapses();
        int[] sourceNeurons = layer.getSourceNeurons();

        out.write(Integer.toString(layersCount));
        out.write(SEPARATOR);
        out.write(Integer.toString(sourceNeurons.length));
        out.write(SEPARATOR);
        out.write(Integer.toString(layer.getSynapsesCount()));
        if (synapses.length > 0) {
            for (int i = 0; i < sourceNeurons.length; i++) {
                out.write(SEPARATOR);
                out.write(Integer.toString(sourceNeurons[i]));

                boolean isFirst = true;
                for (int j = 0; j < synapses.length; j++) {
                    if (synapses[j][0] == i) {
                        if (isFirst) {
                            out.write(SIZE_SEPARATOR);
                            isFirst = false;
                        } else {
                            out.write(VALUE_SEPARATOR);
                        }

                        out.write(Integer.toString(synapses[j][1]));

                    }
                }

            }

        }
        layersCount++;
        out.write(END_TAG);
        out.flush();

    }

    public int getLayersCount() {
        return layersCount;
    }

    public void flush() throws IOException {
        out.flush();
    }

    public void close() throws IOException {
        out.close();
    }
}
